﻿using Assets.Game.scripts.ui.models;
using FrenzyIsland.UI;
using Game.Scripts.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Assets.Game.scripts.ui.controllers {

    [RequireComponent(typeof(PartyItemRendererModel))]
    class PartyItemRendererController : MonoBehaviour {

        public UISprite workStatusSprite;
        public UIWidget frontFaceWidget;
        public UIWidget backFaceWidget;
        public GameObject partyDetailsWidgetPrefab;
        public GameObject partyCouplingWidgetPrefab;
        public List<EventDelegate> onDeleteParty;

        public void Start() {
            PartyItemRendererModel model = this.GetComponent<PartyItemRendererModel>();

            if (this.workStatusSprite) {
                if (model.party.havingFun) {
                    this.workStatusSprite.spriteName = "work-icon";
                } else {
                    this.workStatusSprite.spriteName = "fun-icon";
                }
            }
        }

        public void onRotationTweenCompleted() {
            bool statusFront = this.frontFaceWidget.gameObject.activeSelf;
            bool statusBack = this.backFaceWidget.gameObject.activeSelf;

            NGUITools.SetActive(this.frontFaceWidget.gameObject, !statusFront);
            NGUITools.SetActive(this.backFaceWidget.gameObject, !statusBack);
        }


        public void onClickDetailsButton() {
            UIRoot root = UIRoot.list[0];
            PartyItemRendererModel model = this.GetComponent<PartyItemRendererModel>();

            if (model!=null && root != null && this.partyDetailsWidgetPrefab!=null) {
                GuiManager guiManager = root.GetComponent<GuiManager>();
                UIPanel ownerContainer = UIPanel.Find(this.transform);
                UIPanel parentContainer = UIPanel.Find(ownerContainer.transform.parent);
                UIPopup parentPopuper = parentContainer.gameObject.AddComponent<UIPopup>();
                GameObject popup = guiManager.openPopup(parentPopuper, this.partyDetailsWidgetPrefab);
                PartyItemRendererModel popupModel = popup.GetComponent<PartyItemRendererModel>();
                popupModel.party = model.party;
            }
        }

        public void onClickCouplingButton() {
            UIRoot root = UIRoot.list[0];
            PartyItemRendererModel model = this.GetComponent<PartyItemRendererModel>();

            if (model != null && root != null && this.partyCouplingWidgetPrefab != null) {
                GuiManager guiManager = root.GetComponent<GuiManager>();
                UIPanel ownerContainer = UIPanel.Find(this.transform);
                UIPanel parentContainer = UIPanel.Find(ownerContainer.transform.parent);
                UIPopup parentPopuper = parentContainer.gameObject.AddComponent<UIPopup>();
                GameObject popup = guiManager.openPopup(parentPopuper, this.partyCouplingWidgetPrefab);
                PartyItemRendererModel popupModel = popup.GetComponent<PartyItemRendererModel>();
                popupModel.party = model.party;
            }
        }

        public void onClickDeleteButton() {
            EventDelegate.Execute(this.onDeleteParty);
            Transform parent = this.gameObject.transform.parent;
            NGUITools.Destroy(this.gameObject);
            parent.gameObject.SendMessage("Reposition");
        }

        public void onClickWorkButton() {
            PartyItemRendererModel model = this.GetComponent<PartyItemRendererModel>();

            if (this.workStatusSprite) {
                model.party.havingFun = !model.party.havingFun;

                if (model.party.havingFun) {
                    this.workStatusSprite.spriteName = "work-icon";
                } else {
                    this.workStatusSprite.spriteName = "fun-icon";
                }
            }
        }

    }


}
